package com.jcfk.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.controller.BaseController;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.system.domain.dto.RefRolePermissionMappingDTO;
import com.jcfk.system.domain.po.RefRolePermissionMapping;
import com.jcfk.system.domain.query.RefRolePermissionMappingQuery;
import com.jcfk.system.service.RefRolePermissionMappingService;
import io.swagger.annotations.Api;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色权限管理
 *
 * @author awei
 */
@Api(tags = {"角色权限管理"})
@RestController
@RequestMapping("/refRolePermissionMapping")
public class RefRolePermissionMappingController extends BaseController {

    @Autowired
    private RefRolePermissionMappingService refRolePermissionMappingService;

    /**
     * 查询角色所有权限
     *
     * @param query
     * @return
     */
    @PostMapping("/getPermiss")
    @ResponseBody
    public RetResult<?> getPermiss(@NonNull @RequestBody @Valid RefRolePermissionMappingQuery query) {
        QueryWrapper<RefRolePermissionMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", query.getRoleId()).eq("deleted", 0);
        List<RefRolePermissionMapping> list = refRolePermissionMappingService.list(queryWrapper);
        List<String> ids = list.stream().map(RefRolePermissionMapping::getPermissionId).collect(Collectors.toList());
        return RetResult.ok(ids);
    }


    /**
     * 添加角色权限
     *
     * @param dto
     * @return
     */
    @RequestLogHeader(modular = "角色权限管理", businessType = "查询")
    @PostMapping("/save")
    @ResponseBody
    public RetResult<?> save(@RequestBody RefRolePermissionMappingDTO dto) {
        refRolePermissionMappingService.insert(dto);
        return RetResult.ok();
    }
}
